package com.hwh.daoIm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.hwh.dao.BuyersDao;
import com.hwh.vo.Buyers;

import JDBCUtil.UTIL;

public class BuyersDaoImp implements BuyersDao {

	// 查询全部买家用户
	public ArrayList<Buyers> getBuyers() {

		ArrayList<Buyers> buyers = new ArrayList<Buyers>();
		PreparedStatement pst = null;
		Connection con = UTIL.getConn();

		ResultSet rs = null;
		StringBuffer sql = new StringBuffer("select * from buyers");
		try {
			pst = con.prepareStatement(sql.toString());
			// 返回一个结果集
			rs = pst.executeQuery();
			while (rs.next()) {
				// 商品对象接收结果集的结果
				Buyers buy = new Buyers();
				buy.setBuy_id(rs.getInt("buy_id"));
				buy.setBuy_name(rs.getString("buy_name"));
				buy.setBuy_pwd(rs.getString("buy_pwd"));
				buy.setBuy_phone(rs.getInt("buy_phone"));

				buyers.add(buy);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				UTIL.closeConn(rs, pst, con);
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}

		return buyers;
	}

	// 买家用户登录
	public Buyers buyerLogin(Buyers buyer) {

		Buyers buyers = new Buyers();
		StringBuffer sql = new StringBuffer("select * from buyers where buy_name=? and buy_pwd=?");
		Connection conn = UTIL.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, buyer.getBuy_name());
			ps.setString(2, buyer.getBuy_pwd());
			rs = ps.executeQuery();
			if (rs.next()) {
				buyers = new Buyers(rs.getInt("buy_id"), rs.getString("buy_name"), rs.getString("buy_pwd"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				UTIL.closeConn(rs, ps, conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return buyers;

	}

	// 插入买家用户数据
	public void buyerRegist(Buyers buyer) {

		StringBuffer sql = new StringBuffer("insert into buyers values('',?,?,?)");
		Connection conn = UTIL.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, buyer.getBuy_name());
			ps.setString(2, buyer.getBuy_pwd());
			ps.setInt(3, buyer.getBuy_phone());
			ps.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				UTIL.closeConn(rs, ps, conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	// 通过编号查询买家信息
	public Buyers getBuyersBuyid(int buy_id) {

		Buyers buyers = new Buyers();
		PreparedStatement pst = null;
		Connection con = UTIL.getConn();

		ResultSet rs = null;
		StringBuffer sql = new StringBuffer("select * from buyers");
		try {
			pst = con.prepareStatement(sql.toString());
			// 返回一个结果集
			rs = pst.executeQuery();
			while (rs.next()) {
				// 商品对象接收结果集的结果

				buyers.setBuy_id(rs.getInt("buy_id"));
				buyers.setBuy_name(rs.getString("buy_name"));
				buyers.setBuy_pwd(rs.getString("buy_pwd"));
				buyers.setBuy_phone(rs.getInt("buy_phone"));

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				UTIL.closeConn(rs, pst, con);
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}

		return buyers;
	}

	// 通过编号修改买家信息
	public void upBuyersBuyid(Buyers buyer) {

		/* Buyers buyers = new Buyers(); */
		PreparedStatement pst = null;
		Connection con = UTIL.getConn();

		ResultSet rs = null;
		StringBuffer sql = new StringBuffer("update buyers set buy_name=?,buy_pwd=?,buy_phone=? where buy_id=?");
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setString(1, buyer.getBuy_name());
			pst.setString(2, buyer.getBuy_pwd());
			pst.setInt(3, buyer.getBuy_phone());
			pst.setInt(4, buyer.getBuy_id());
			
			pst.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				UTIL.closeConn(rs, pst, con);
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}

		
	}

}
